/**
 * 手机号码验证
 */
export const isMobile = it => {
  return /^0?(13[0-9]|14[456789]|15[012356789]|16[0678]|17[0-9]|18[0-9]|19[89])[0-9]{8}$/.test(
    it
  )
}
/**
 * 邮箱验证
 */
export const isEmail = it => {
  return /^([A-Za-z0-9_\-\.\u4e00-\u9fa5])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/.test(
    it
  )
}
/**
 * 网址url验证
 */
export const isUrl = str_url => {
  let urlRexp = /(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/
  return urlRexp.test(str_url)
}

/**
 * 双位数字 小于10补0
 */
export const twodigit = val => {
  return `0${val}`.slice(-2)
}

/**
 * 日期格式化
 */
export const dateFormat = (date, format) => {
  let formatStr = format || 'yyyy-MM-dd HH:mm:ss'
  date = date || new Date()
  if (typeof date === 'string') {
    formatStr = date
    date = new Date()
  }
  var str = formatStr
  str = str.replace(/yyyy|YYYY/, date.getFullYear())
  str = str.replace(
    /yy|YY/,
    date.getYear() % 100 > 9
      ? (date.getYear() % 100).toString()
      : '0' + (date.getYear() % 100)
  )
  str = str.replace(/MM/, twodigit(date.getMonth() + 1))
  str = str.replace(/M/g, date.getMonth() + 1)
  str = str.replace(/dd|DD/, twodigit(date.getDate()))
  str = str.replace(/d|D/g, date.getDate())
  str = str.replace(/hh|HH/, twodigit(date.getHours()))
  str = str.replace(/h|H/g, date.getHours())
  str = str.replace(/mm/, twodigit(date.getMinutes()))
  str = str.replace(/m/g, date.getMinutes())
  str = str.replace(/ss|SS/, twodigit(date.getSeconds()))
  str = str.replace(/s|S/g, date.getSeconds())
  return str
}
